import api from '@/assets/js/api';
import {systemConfig} from '@/assets/js/utils';//接口API
import Weixin from 'weixin-js-sdk';

function initConfig(jsApiList, fn) {
  if (!(navigator.userAgent.indexOf('MicroMessenger') > -1)) return;
  api.fetch("/api/wechat/js", {
    params: {
      url: location.href.split("#")[0]
    }
  }).then(response => {
    const data = response.data;
    if (jsApiList) {
      data.jsApiList = jsApiList;
    } else {
      data.jsApiList = ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone', 'chooseWXPay'];
    }
    Weixin.config(data);
    fn && fn();
  });
}

export default{
  //隐藏
  hide: () => {
    initConfig(null, () => {
      Weixin.hideOptionMenu();
    });
  },
  //支付
  pay: (data) => {
    initConfig(null, () => {
      Weixin.chooseWXPay(data);
    });
  },
  //分享
  share: (data) => {
    initConfig(null, () => {
      Weixin.showOptionMenu();
      systemConfig(config => {
        let opts = {
          title: config.share_title.length <= 11 ? config.share_title : config.share_title.substr(0, 11) + '...',
          desc: config.share_description.length <= 35 ? config.share_description : config.share_description.substr(0, 35) + '...',
          link: window.location.protocol + '//' + window.location.host + '/?#',
          imgUrl: config.share_image,
          type: "",
          dataUrl: "",
          success: null,
          cancel: null,
          fail: null
        };
        if (data) {
          for (let key in data) {
            opts[key] = data[key];
          }
        }
        Weixin.ready(() => {
          //分享到朋友圈
          Weixin.onMenuShareTimeline({
            title: opts.title,
            link: opts.link,
            imgUrl: opts.imgUrl,
            success: function () {
              opts.success && opts.success();
            },
            cancel: function () {
              opts.cancel && opts.cancel();
            }
          });
          //分享给朋友
          Weixin.onMenuShareAppMessage({
            title: opts.title,
            desc: opts.desc,
            link: opts.link,
            imgUrl: opts.imgUrl,
            type: opts.type,
            dataUrl: opts.dataUrl,
            success: function () {
              opts.success && opts.success();
            },
            cancel: function () {
              opts.cancel && opts.cancel();
            }
          });
          //分享到QQ
          Weixin.onMenuShareQQ({
            title: opts.title, // 分享标题
            desc: opts.desc, // 分享描述
            link: opts.link, // 分享链接
            imgUrl: opts.imgUrl, // 分享图标
            success: function () {
              opts.success && opts.success();
            },
            cancel: function () {
              opts.cancel && opts.cancel();
            }
          });
          //分享到腾讯微博
          Weixin.onMenuShareWeibo({
            title: opts.title, // 分享标题
            desc: opts.desc, // 分享描述
            link: opts.desc, // 分享链接
            imgUrl: opts.imgUrl, // 分享图标
            success: function () {
              opts.success && opts.success();
            },
            cancel: function () {
              opts.cancel && opts.cancel();
            }
          });
          //分享到QQ空间
          Weixin.onMenuShareQZone({
            title: opts.title, // 分享标题
            desc: opts.desc, // 分享描述
            link: opts.desc, // 分享链接
            imgUrl: opts.imgUrl, // 分享图标
            success: function () {
              opts.success && opts.success();
            },
            cancel: function () {
              opts.cancel && opts.cancel();
            }
          });
        });
      })
    });
  }
}

